bittorrent-tracker
Simple, robust, BitTorrent tracker (client & server) implementation
Node.js implementation of a BitTorrent tracker, client and server.
A BitTorrent tracker is a web service which responds to requests from BitTorrent
clients. The requests include metrics from clients that help the tracker keep overall
statistics about the torrent. The response includes a peer list that helps the client
participate in the torrent swarm.
This module is used by WebTorrent.
features
- Includes client & server implementations
- Supports all mainstream tracker types:
- Supports ipv4 & ipv6
- Supports tracker "scrape" extension
- Robust and well-tested
- Tracker statistics available via web interface at
/stats
or JSON data at /stats.json
Also see bittorrent-dht.
Tracker stats
install
npm install bittorrent-tracker
usage
client
To connect to a tracker, just do this:
var Client = require('bittorrent-tracker')
var requiredOpts = {
infoHash: new Buffer('012345678901234567890'),
peerId: new Buffer('01234567890123456789'),
announce: [],
port: 6881
}
var optionalOpts = {
rtcConfig: {},
userAgent: '',
wrtc: {},
getAnnounceOpts: function () {
return {
uploaded: 0,
downloaded: 0,
left: 0,
customParam: 'blah'
}
},
proxyOpts: {
socksProxy: {
proxy: {
ipaddress: "1.2.3.4",
port: 1080,
type: 5,
userid: "someuserid",
authentication: {
username: "Josh",
password: "somepassword"
}
},
timeout: 10000
},
httpAgent: {},
httpsAgent: {}
},
}
var client = new Client(requiredOpts)
client.on('error', function (err) {
console.log(err.message)
})
client.on('warning', function (err) {
console.log(err.message)
})
client.start()
client.on('update', function (data) {
console.log('got an announce response from tracker: ' + data.announce)
console.log('number of seeders in the swarm: ' + data.complete)
console.log('number of leechers in the swarm: ' + data.incomplete)
})
client.once('peer', function (addr) {
console.log('found a peer: ' + addr)
})
client.complete()
client.update()
client.update({
uploaded: 0,
downloaded: 0,
left: 0,
customParam: 'blah'
})
client.stop()
client.destroy()
client.scrape()
client.on('scrape', function (data) {
console.log('got a scrape response from tracker: ' + data.announce)
console.log('number of seeders in the swarm: ' + data.complete)
console.log('number of leechers in the swarm: ' + data.incomplete)
console.log('number of total downloads of this torrent: ' + data.downloaded)
})
server
To start a BitTorrent tracker server to track swarms of peers:
const Server = require('bittorrent-tracker').Server
const server = new Server({
udp: true,
http: true,
ws: true,
stats: true,
trustProxy: false,
filter: function (infoHash, params, cb) {
const allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa')
if (allowed) {
cb(null)
} else {
cb(new Error('disallowed torrent'))
}
}
})
server.http
server.udp
server.ws
server.on('error', function (err) {
console.log(err.message)
})
server.on('warning', function (err) {
console.log(err.message)
})
server.on('listening', function () {
const httpAddr = server.http.address()
const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost'
const httpPort = httpAddr.port
console.log(`HTTP tracker: http://${httpHost}:${httpPort}/announce`)
const udpAddr = server.udp.address()
const udpHost = udpAddr.address
const udpPort = udpAddr.port
console.log(`UDP tracker: udp://${udpHost}:${udpPort}`)
const wsAddr = server.ws.address()
const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost'
const wsPort = wsAddr.port
console.log(`WebSocket tracker: ws://${wsHost}:${wsPort}`)
})
const port = 0
const hostname = "localhost"
server.listen(port, hostname, () => {
})
server.on('start', function (addr) {
console.log('got start message from ' + addr)
})
server.on('complete', function (addr) {})
server.on('update', function (addr) {})
server.on('stop', function (addr) {})
Object.keys(server.torrents)
server.torrents[infoHash].complete
server.torrents[infoHash].incomplete
server.torrents[infoHash].peers
The http server will handle requests for the following paths: /announce
, /scrape
. Requests for other paths will not be handled.
multi scrape
Scraping multiple torrent info is possible with a static Client.scrape
method:
var Client = require('bittorrent-tracker')
Client.scrape({ announce: announceUrl, infoHash: [ infoHash1, infoHash2 ]}, function (err, results) {
results[infoHash1].announce
results[infoHash1].infoHash
results[infoHash1].complete
results[infoHash1].incomplete
results[infoHash1].downloaded
})
command line
Install bittorrent-tracker
globally:
$ npm install -g bittorrent-tracker
Easily start a tracker server:
$ bittorrent-tracker
http server listening on 8000
udp server listening on 8000
ws server listening on 8000
Lots of options:
$ bittorrent-tracker --help
bittorrent-tracker - Start a bittorrent tracker server
Usage:
bittorrent-tracker [OPTIONS]
If no --http, --udp, or --ws option is supplied, all tracker types will be started.
Options:
-p, --port [number] change the port [default: 8000]
--trust-proxy trust 'x-forwarded-for' header from reverse proxy
--interval client announce interval (ms) [default: 600000]
--http enable http server
--udp enable udp server
--ws enable websocket server
-q, --quiet only show error output
-s, --silent show no output
-v, --version print the current version
license
MIT. Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC.